/**
 * Editor Dialog fo a Multimedia content
 * 
 * @author remo.lemma@lu.unisi.ch & patrick.zulian@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui.itemeditor.multimedia;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import ch.unisi.inf.projects.java.uom.gui.UOMImages;
import ch.unisi.inf.projects.java.uom.gui.UOMMainFrame;
import ch.unisi.inf.projects.java.uom.gui.commands.IconedCommand;

import ch.unisi.inf.projects.java.uom.elements.*;

public final class UOMMultimediaEditorDialog extends JDialog {

    /** We are not going to serialize this class atm */
    private static final long serialVersionUID = -1L;
    
    private final Multimedia multimedia;
	
	public UOMMultimediaEditorDialog (final Multimedia multimedia)
	{
		super();
		this.setTitle("Edit this content");
		this.multimedia = multimedia;
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        UOMMainFrame.instance().setEnabled(false);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosed (final WindowEvent ev) { UOMMainFrame.instance().setEnabled(true); }
		});
        makeDialog();
        this.getContentPane().setLayout(new BorderLayout());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
	}
	
	private void makeDialog ()
	{
		final JPanel panel = new JPanel(new GridLayout(0,2));
		final JLabel tlabel = new JLabel("Title");
		panel.add(tlabel);
		final JTextField ttext = new JTextField(multimedia.getTitle());
		adjustText(ttext);
		panel.add(ttext);
		final JLabel ylabel = new JLabel("Type");
		panel.add(ylabel);
		final JTextField ytext = new JTextField(multimedia.getType());
		ytext.setEditable(false);
		adjustText(ytext);
		panel.add(ytext);
		final JLabel ulabel = new JLabel("URL");
		panel.add(ulabel);
		final JTextField utext = new JTextField(multimedia.getURL());
		utext.setEditable(false);
		adjustText(utext);
		panel.add(utext);
		this.getContentPane().add(panel, BorderLayout.NORTH);
		final JLabel clabel = new JLabel("Comment");
		panel.add(clabel);
		final JTextArea area = new JTextArea(multimedia.getComment());
		area.setAutoscrolls(true);
		area.setCaretPosition(0);
		area.setLineWrap(true);
		area.setWrapStyleWord(true);
		area.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		final JScrollPane scrollPane = new JScrollPane(area);
		scrollPane.setPreferredSize(new Dimension(200, 90));
		scrollPane.setMinimumSize(area.getPreferredSize());
		scrollPane.setMaximumSize(area.getPreferredSize());
		this.getContentPane().add(scrollPane, BorderLayout.CENTER);
		final JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(Box.createHorizontalGlue());
		final JButton saveButton = new JButton(new IconedCommand("", "Save", UOMImages.OK.getFileName()) {
			private static final long serialVersionUID = -1L;
			public void actionPerformed(ActionEvent e) {
                multimedia.setTitle(ttext.getText());
                final int limit = Math.min(254,area.getText().length());
                multimedia.setComment(area.getText().substring(0,limit)); // Limit!
                multimedia.setModified(true);
                dispose();
			}
		});
		buttonPanel.add(saveButton);
        pack();
		final JButton exitButton = new JButton(new IconedCommand("", "Cancel", UOMImages.CANCEL.getFileName()) {
			private static final long serialVersionUID = -1L;
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });		
        buttonPanel.add(Box.createHorizontalStrut(10));
        buttonPanel.add(exitButton);
        buttonPanel.add(Box.createHorizontalStrut(5));
        this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        pack();
	}
	
	private void adjustText (final JTextField textField)
	{
		textField.setPreferredSize(new Dimension(200,30));
        textField.setMinimumSize(textField.getPreferredSize());
        textField.setMaximumSize(textField.getPreferredSize());
        textField.setCaretPosition(0);
	}
}
